<?php
class FinanceController extends BaseController
{
    public function yesterdayAction()
    {
        $yesterday_start = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
        $yesterday_end   = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
        $db              = $this->Mysql();
        $res             = $db->has('aggregation_finance', [
            'createtime' => $yesterday_start,
        ]);
        if ($res == true) {
            echo '今日已统计';
            return;
        }

        // 昨日总订单
        $total_order = $db->count('aggregation_order', '*', [
            'createtime[<>]' => [$yesterday_start, $yesterday_end],
        ]);
        $data['total_order'] = is_null($total_order) ? 0 : $total_order;
        // 昨日完成订单
        $ok_total_order = $db->count('aggregation_order', '*', [
            'createtime[<>]' => [$yesterday_start, $yesterday_end],
            'status'         => '充值完成',
        ]);
        $data['ok_total_order'] = is_null($ok_total_order) ? 0 : $ok_total_order;
 $error_total_order = $db->count('aggregation_order', '*', [
            'createtime[<>]' => [$yesterday_start, $yesterday_end],
            'status'         => '充值失败',
        ]);
        $data['error_total_order'] = is_null($error_total_order) ? 0 : $error_total_order;
        // 昨日完成订单
        $error_total_order = $db->count('aggregation_order', '*', [
            'createtime[<>]' => [$yesterday_start, $yesterday_end],
            'status'         => '充值失败',
        ]);
        $data['error_total_order'] = is_null($error_total_order) ? 0 : $error_total_order;
        // 昨日总支出
        $pay = $db->sum('aggregation_order', 'amount', [
            'createtime[<>]' => [$yesterday_start, $yesterday_end],
            'status'         => '充值完成',
        ]);
        $data['pay'] = is_null($pay) ? 0 : $pay;
        // 昨日总收入
        $income = $db->sum('aggregation_order', 'buying_price', [
            'createtime[<>]' => [$yesterday_start, $yesterday_end],
            'status'         => '充值完成',
        ]);
        $data['income']     = is_null($income) ? 0 : $income;
        $data['profit']     =   $data['pay']-$data['income'];
        $data['createtime'] = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));

        $db->insert('aggregation_finance', $data);
    }

    public function todayAction()
    {

        $db    = $this->Mysql();
        $today = strtotime(date('Y-m-d'));
        $res   = $db->has('aggregation_finance', [
            'createtime' => $today,
        ]);

        // 昨日总订单
        $total_order = $db->count('aggregation_order', '*', [
            'createtime[<>]' => [$today, $today + 86400],
        ]);
        $data['total_order'] = is_null($total_order) ? 0 : $total_order;
        // 昨日完成订单
        $ok_total_order = $db->count('aggregation_order', '*', [
            'createtime[<>]' => [$today, $today + 86400],
            'status'         => '充值完成',
        ]);
        $data['ok_total_order'] = is_null($ok_total_order) ? 0 : $ok_total_order;
        $error_total_order = $db->count('aggregation_order', '*', [
            'createtime[<>]' => [$today, $today + 86400],
            'status'         => '充值失败',
        ]);
        $data['error_total_order'] = is_null($error_total_order) ? 0 : $error_total_order;
        // 昨日总支出
        $pay = $db->sum('aggregation_order', 'amount', [
            'createtime[<>]' => [$today, $today + 86400],
            'status'         => '充值完成',
        ]);
        $data['pay'] = is_null($pay) ? 0 : $pay;
        // 昨日总收入
        $income = $db->sum('aggregation_order', 'buying_price', [
            'createtime[<>]' => [$today, $today + 86400],
            'status'         => '充值完成',
        ]);
        $data['income']     = is_null($income) ? 0 : $income;
        $data['profit']     =  $data['pay']- $data['income'];
        $data['createtime'] = $today;
        if ($res == true) {
            echo '更新今日数据成功';
            $db->update('aggregation_finance', $data, [
                'createtime' => $today,
            ]);
        } else {
            echo '添加成功';
            $db->insert('aggregation_finance', $data);
        }

    }

    public function storeyesterdayAction()
    {
        $yesterday_start = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
        $yesterday_end   = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
        $db              = $this->Mysql();
        $aggregation_authorize = $db->select('aggregation_authorize','*',[
            'deletetime'=>null
        ]);
    
        foreach ($aggregation_authorize as $key => $value) {

             $res             = $db->has('aggregation_store_finance', [
                'createtime' => $yesterday_start,
                'authorize_number'=>$value['authorize_number'],
            ]);

            if ($res == true) {
                echo '今日已统计';
                continue;
            }
            // 昨日总订单
            $total_order = $db->count('aggregation_order', '*', [
                'authorize_number'=>$value['authorize_number'],
                'createtime[<>]' => [$yesterday_start, $yesterday_end],
            ]);
            $data['total_order'] = is_null($total_order) ? 0 : $total_order;
        $error_total_order = $db->count('aggregation_order', '*', [
            'createtime[<>]' => [$yesterday_start, $yesterday_end],
            'status'         => '充值失败',
        ]);
        $data['error_total_order'] = is_null($error_total_order) ? 0 : $error_total_order;

            // 昨日完成订单
            $ok_total_order = $db->count('aggregation_order', '*', [
                'authorize_number'=>$value['authorize_number'],
                'createtime[<>]' => [$yesterday_start, $yesterday_end],
                'status'         => '充值完成',
            ]);
            $data['ok_total_order'] = is_null($ok_total_order) ? 0 : $ok_total_order;
            // 昨日总支出
            $pay = $db->sum('aggregation_order', 'amount', [
                'authorize_number'=>$value['authorize_number'],
                'createtime[<>]' => [$yesterday_start, $yesterday_end],
                'status'         => '充值完成',
            ]);
            $data['pay'] = is_null($pay) ? 0 : $pay;

            $data['createtime'] = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
            $data['authorize_number'] = $value['authorize_number'];
            $db->insert('aggregation_store_finance', $data);
        }
       
  
       

        


        
    }


}
